﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using grproLib;
using System.Reflection;
using Zwms.Data.BusinessObjects;
using Zwms.Client.Helper;
using Zwms.Data.CommonObject;

namespace Zwms.Client.Report
{
    class ReportHelper
    {
        public static void FillRecordToReport<T>(IGridppReport report, T myClass)
        {
            foreach (IGRField fld in report.DetailGrid.Recordset.Fields)
            {
                foreach (PropertyInfo pi in myClass.GetType().GetProperties())
                {
                    if (String.Compare(fld.RunningDBField, pi.Name, true) == 0)
                    {
                        report.DetailGrid.Recordset.Append();
                        fld.Value = pi.GetValue(myClass, null);
                        report.DetailGrid.Recordset.Post();
                    }
                }
            }
        }

        public static void FillRecordToReport(IGridppReport report, Dictionary<string, string> dict)
        {
            report.DetailGrid.Recordset.Append();
            foreach (IGRField fld in report.DetailGrid.Recordset.Fields)
            {
                foreach (KeyValuePair<string, string> kv in dict)
                {
                    if (String.Compare(fld.RunningDBField, kv.Key, true) == 0)
                    {

                        fld.Value = kv.Value;

                    }
                }
            }
            report.DetailGrid.Recordset.Post();
        }

        public static void FillRecordToReport(IGridppReport report, List<Dictionary<string, string>> dictList)
        {

            foreach (Dictionary<string, string> dict in dictList)
            {
                report.DetailGrid.Recordset.Append();
                foreach (IGRField fld in report.DetailGrid.Recordset.Fields)
                {
                    foreach (KeyValuePair<string, string> kv in dict)
                    {
                        if (String.Compare(fld.RunningDBField, kv.Key, true) == 0)
                        {

                            fld.Value = kv.Value;

                        }
                    }
                }
                report.DetailGrid.Recordset.Post();
            }

        }

        public static void PrintInPlanBill(BilInPlanBill record,List<BilInPlanBillDetail> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (BilInPlanBillDetail detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("IBPlanCode", record.Id);
                reportDict.Add("IBType", CommonDefine.DictIBType[record.IBType]);
                reportDict.Add("SenderName", record.SenderName);
                reportDict.Add("RecieverName", record.RecieverName);
                if (!string.IsNullOrWhiteSpace(record.PlanReceiveDate))
                {
                    reportDict.Add("PlanReceiveDate", string.Format("{0:yyyy-MM-dd}", CommonHelper.GetDatetimeFromString(record.PlanReceiveDate)));
                }
                else
                {
                    reportDict.Add("PlanReceiveDate", "");
                } 
                reportDict.Add("BillStatus", CommonDefine.DictIPBillStatus[record.BillStatus]);
                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("SingleGoodsCode", detail.SingleGoodsCode);
                reportDict.Add("GoodsSpec", detail.GoodsSpec.ToString());
                reportDict.Add("GoodsPackageQty", detail.GoodsPackageQty.ToString());
                reportDict.Add("GoodsPieceQty", detail.GoodsPieceQty.ToString());
                reportDict.Add("GoodsQty", detail.GoodsQty.ToString());

                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportInPlanBill, dictList);
            window.Show();
        }

        public static void PrintInBill(BilInBill record, List<BilInBillDetail> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (BilInBillDetail detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("IBCode", record.Id);
                reportDict.Add("IBPlanCode", record.IBPlanCode);
                reportDict.Add("IBType", CommonDefine.DictIBType[record.IBType]);
                reportDict.Add("WarehouseName", record.WarehouseName);
                reportDict.Add("SenderName", record.SenderName);
                reportDict.Add("RecieverName", record.RecieverName);
                if (!string.IsNullOrWhiteSpace(record.ReceiveDate))
                {
                    reportDict.Add("ReceiveDate", string.Format("{0:yyyy-MM-dd}", CommonHelper.GetDatetimeFromString(record.ReceiveDate)));
                }
                else
                {
                    reportDict.Add("ReceiveDate", "");
                }

                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("SingleGoodsCode", detail.SingleGoodsCode);
                reportDict.Add("GoodsSpec", detail.GoodsSpec.ToString());
                reportDict.Add("GoodsPackageQty", detail.GoodsPackageQty.ToString());
                reportDict.Add("GoodsPieceQty", detail.GoodsPieceQty.ToString());
                reportDict.Add("ReceiveGoodsPackageQty", detail.ReceiveGoodsPackageQty.ToString());
                reportDict.Add("ReceiveGoodsPieceQty", detail.ReceiveGoodsPieceQty.ToString());
                reportDict.Add("DiffGoodsQty", Convert.ToString(detail.GoodsQty-detail.ReceiveGoodsQty));

                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportInBill, dictList);
            window.Show();
        }

        public static void PrintOutBill(BilOutBill record, List<BilOutBillDetail> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            ServiceReferenceZwms.ServiceZwmsClient client = new ServiceReferenceZwms.ServiceZwmsClient();
            decimal sumWeight = 0;
            decimal sumCost = 0;
            foreach (BilOutBillDetail detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("OBCode", record.Id);
                reportDict.Add("OBPlanCode", record.OBPlanCode);
                reportDict.Add("OutType", CommonDefine.DictOBType[record.OutType]);
                reportDict.Add("WarehouseName", record.WarehouseName);
                reportDict.Add("SenderName", record.SenderName);
                reportDict.Add("RecieverName", record.RecieverName);
                if (!string.IsNullOrWhiteSpace(record.OutDate))
                {
                    reportDict.Add("OutDate", string.Format("{0:yyyy-MM-dd}", CommonHelper.GetDatetimeFromString(record.OutDate)));
                }
                else
                {
                    reportDict.Add("OutDate", "");
                }

                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("SingleGoodsCode", detail.SingleGoodsCode);
                reportDict.Add("GoodsSpec", detail.GoodsSpec.ToString());
                reportDict.Add("GoodsPackageQty", detail.GoodsPackageQty.ToString());
                reportDict.Add("GoodsPieceQty", detail.GoodsPieceQty.ToString());
                reportDict.Add("GoodsQty", detail.GoodsQty.ToString());
                reportDict.Add("OutGoodsPackageQty", detail.OutGoodsPackageQty.ToString());
                reportDict.Add("OutGoodsPieceQty", detail.OutGoodsPieceQty.ToString());
                reportDict.Add("OutGoodsQty", detail.OutGoodsQty.ToString());
                reportDict.Add("DiffGoodsQty", Convert.ToString(detail.GoodsQty - detail.OutGoodsQty));

                dictList.Add(reportDict);
                //计算重量
                BaseGood queryGood = new BaseGood();
                queryGood.Id = detail.GoodsCode;
               queryGood =  client.QueryGoods(queryGood);
               if (queryGood != null)
               {
                   sumWeight += queryGood.PieceWeight * detail.OutGoodsQty;
                   sumCost += queryGood.RetailPrice * detail.OutGoodsQty;
               }
               reportDict.Add("SumWeight", Math.Round(sumWeight, 3).ToString());
               reportDict.Add("SumCost", Math.Round(sumCost, 3).ToString());
            }
          
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportOutBill, dictList);
            window.Show();
        }

        public static void PrintLocationArrange(List<StoGoodsStock> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (StoGoodsStock detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("LocationName", detail.LocationName);
                reportDict.Add("ShelfName", detail.ShelfName);
                reportDict.Add("AreaName", detail.AreaName);
                reportDict.Add("PackageQty", detail.PackageQty.ToString());
                reportDict.Add("PieceQty", detail.PieceQty.ToString());
                reportDict.Add("GoodsTotalQty", detail.GoodsTotalQty.ToString());
                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportLocationArrange, dictList);
            window.Show();
        }

        public static void PrintGoodsStock(List<StoGoodsStock> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (StoGoodsStock detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("LocationName", detail.LocationName);
                reportDict.Add("ShelfName", detail.ShelfName);
                reportDict.Add("AreaName", detail.AreaName);
                reportDict.Add("PackageQty", detail.PackageQty.ToString());
                reportDict.Add("PieceQty", detail.PieceQty.ToString());
                reportDict.Add("GoodsTotalQty", detail.GoodsTotalQty.ToString());

                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportGoodsStock, dictList);
            window.Show();
        }

        public static void PrintPickBill(BilOutPlanBill record, List<BilOutBillDetail> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (BilOutBillDetail detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("OBPlanCode", record.Id);
                reportDict.Add("OutType", CommonDefine.DictOBType[record.OutType]);
                reportDict.Add("OBCode", detail.OBCode);
                reportDict.Add("SenderName", record.SenderName);
                reportDict.Add("RecieverName", record.RecieverName); 
                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsBarCode", detail.GoodsBarCode);
                reportDict.Add("SingleGoodsCode", detail.SingleGoodsCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("LocationName", detail.LocationName);
                reportDict.Add("ShelfName", detail.ShelfName);
                reportDict.Add("AreaName", detail.AreaName);
                reportDict.Add("GoodsPackageQty", detail.GoodsPackageQty.ToString());
                reportDict.Add("GoodsPieceQty", detail.GoodsPieceQty.ToString());
                reportDict.Add("GoodsQty", detail.GoodsQty.ToString());

                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportPickBill, dictList);
            window.Show();
        }

        public static void PrintCountBill(BilCountBill record, List<BilCountBillDetail> detailList)
        {
            List<Dictionary<string, string>> dictList = new List<Dictionary<string, string>>();
            foreach (BilCountBillDetail detail in detailList)
            {
                Dictionary<string, string> reportDict = new Dictionary<string, string>();
                reportDict.Add("CountBillCode", record.Id);
                reportDict.Add("WarehouseName", record.WarehouseName);
                reportDict.Add("CountOperatorName", record.CountOperatorName);
                if (!string.IsNullOrWhiteSpace(record.PlanCountDate))
                {
                    reportDict.Add("PlanCountDate", string.Format("{0:yyyy-MM-dd}", CommonHelper.GetDatetimeFromString(record.PlanCountDate)));
                }
                else
                {
                    reportDict.Add("PlanCountDate", "");
                }
                if (!string.IsNullOrWhiteSpace(record.CountDate))
                {
                    reportDict.Add("CountDate", string.Format("{0:yyyy-MM-dd}", CommonHelper.GetDatetimeFromString(record.CountDate)));
                }
                else
                {
                    reportDict.Add("CountDate", "");
                }
                reportDict.Add("GoodsCode", detail.GoodsCode);
                reportDict.Add("GoodsName", detail.GoodsName);
                reportDict.Add("LocationCode", detail.LocationCode);
                reportDict.Add("Spec", detail.Spec.ToString());
                reportDict.Add("PkgQty", detail.PkgQty.ToString());
                reportDict.Add("CountedPkgQty", detail.CountedPkgQty.ToString());
                reportDict.Add("PieceQty", detail.PieceQty.ToString());
                reportDict.Add("CountedPieceQty", detail.CountedPieceQty.ToString());
                reportDict.Add("GoodsQty", detail.GoodsQty.ToString());
                reportDict.Add("CountedGoodsQty", Convert.ToString(detail.CountedPkgQty * detail.Spec + detail.CountedPieceQty));
                reportDict.Add("DiffQty", Convert.ToString(detail.CountedPkgQty * detail.Spec + detail.CountedPieceQty - detail.GoodsQty));

                dictList.Add(reportDict);
            }
            ReportPrintForm window = new ReportPrintForm(StringHelper.sReportCountBill, dictList);
            window.Show();
        }
    }
}
